package com.new2i.util;

/**
 * 正则表达式工具类
 * 
 * @since 2015/09/04
 * @author 2i
 *
 */
public class RegexUtil {

	/**
	 * 字符串匹配校验
	 * 
	 * @since 2015/09/04
	 * @author 2i
	 * @param str
	 * @param regex
	 * @return
	 */
	public static boolean validateByRegex(String str, String regex) {
		boolean rtnValue = false;
		if (str == null || regex == null)
			return false;
		try {
			rtnValue = str.matches(regex);
		} catch (Exception e) {
			return false;
		}
		return rtnValue;
	}

	/**
	 * doctor_id匹配校验
	 * 
	 * @since 2015/09/04
	 * @author 2i
	 * @param str
	 * @return
	 */
	public static boolean validateDoctorId(String str) {
		String regex = "1[0-9]{4}";
		return validateByRegex(str, regex);
	}

	/**
	 * 电话号码匹配校验
	 * 
	 * @since 2015/09/04
	 * @author 2i
	 * @param str
	 * @return
	 */
	public static boolean validatePhoneNum(String str) {
		String regex = "1[34578][0-9]{9}";
		return validateByRegex(str, regex);
	}

	/**
	 * 加密后的md5匹配校验
	 * 
	 * @since 2015/09/04
	 * @author 2i
	 * @param str
	 * @return
	 */
	public static boolean validateEncodeMD5(String str) {
		String regex = "[0-9][A-Za-z0-9]{64}";
		return validateByRegex(str, regex);
	}

	/**
	 * 浮点数校验
	 * 
	 * @since 2015/09/16
	 * @author 2i
	 * @param str
	 * @return
	 */
	public static boolean validateFloatValue(String str) {
		String regex = "^\\d+\\.\\d+$";
		return validateByRegex(str, regex);
	}

	/**
	 * 浮点数校验
	 * 
	 * @since 2015/09/16
	 * @author 2i
	 * @param str
	 * @return
	 */
	public static boolean validateFixedDecimalFloatValue(String str, int precision) {
		String regex = "^\\d+\\.\\d{" + precision + "}$";
		return validateByRegex(str, regex);
	}

	/**
	 * 浮点数校验
	 * 
	 * @since 2015/09/16
	 * @author 2i
	 * @param str
	 * @param minPrecision
	 * @param maxPrecision
	 * @return
	 */
	public static boolean validateFloatValue(String str, int minPrecision, int maxPrecision) {
		String regex = "^\\d+\\.\\d{" + minPrecision + "," + maxPrecision + "}$";
		return validateByRegex(str, regex);
	}
	 
	/**
	 * 
	 * 浮点数校验
	 * 
	 * @since 2015/09/16
	 * @author 2i
	 * @param str
	 * @param maxPrecision
	 * @return
	 */
	public static boolean validateFloatValue(String str, int maxPrecision) { 
		return validateFloatValue(str, 0, maxPrecision);
	}
}
